package cn.ekuma.pos.toolcommand.develop;

import javax.swing.JPanel;
import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import cn.ekuma.data.dao.bean.IKeyed;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import com.openbravo.bean.Location;
import com.openbravo.bean.Product;
import com.openbravo.bean.ProductCategory;
import com.openbravo.bean.erp.OperationProductCategory;
import com.openbravo.bean.infobean.CustomerInfo;
import com.openbravo.bean.infobean.UserInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.event.CustomerChangedListener;
import com.openbravo.pos.panels.event.LocationChangedListener;
import com.openbravo.pos.panels.event.ProductCategoryChangedListener;
import com.openbravo.pos.panels.event.ProductChangedListener;
import com.openbravo.pos.panels.event.UserChangedListener;
import com.openbravo.pos.panels.link.JSeachProductPanel;
import com.openbravo.pos.panels.navigator.JCustomerNavigator;
import com.openbravo.pos.panels.navigator.JLocationNavigator;
import com.openbravo.pos.panels.navigator.JOperationProductCategoryNavigator;
import com.openbravo.pos.panels.navigator.JProductCategoryNavigator;
import com.openbravo.pos.panels.navigator.JUserNavigator;
import com.openbravo.pos.panels.navigator.event.OperationProductCategoryChangedListener;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;

public class JShowBussObjectIdPanel extends JPanel{

	protected I_DataLogicERP dlSales;
	private CardLayout cl1;
	private JProductCategoryNavigator productCategoryNavigator;
	private JTextArea textArea;
	
	/**
	 * Create the panel.
	 */
	public JShowBussObjectIdPanel(I_DataLogicERP dlSales) {
		setPreferredSize(new Dimension(650, 530));
		this.dlSales=dlSales;
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		
		JPanel jNavigator = new JPanel();
		add(jNavigator, BorderLayout.EAST);
		jNavigator.setLayout(new CardLayout(0, 0));
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		add(splitPane, BorderLayout.CENTER);
		
		JScrollPane scrollPane = new JScrollPane();
		splitPane.setLeftComponent(scrollPane);
		
		 textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		splitPane.setRightComponent(tabbedPane);
		cl1 = (CardLayout) (jNavigator.getLayout());
		
		configNavigator(panel,jNavigator);
		
		configTabPanel(tabbedPane);
		splitPane.setDividerLocation(400);
	}
	

	protected  void configNavigator(JPanel navigatorToolBar, final JPanel jNavigator){
		productCategoryNavigator = new JProductCategoryNavigator(dlSales);
		productCategoryNavigator.addProductCategoryChangedListener(new ProductCategoryChangedListener(){
			
			public void valueSelectChanged(ProductCategory obj) {
				textArea.setText(textArea.getText()+obj.getID()+"-->"+obj.getName()+"\n");
			}
			
		});
		jNavigator.add(productCategoryNavigator, "category");

		JOperationProductCategoryNavigator operationProductCategoryNavigator = new JOperationProductCategoryNavigator(
				dlSales);
		operationProductCategoryNavigator
				.addOperationProductCategoryChangedListener(new OperationProductCategoryChangedListener(){

					
					public void valueSelectChanged(OperationProductCategory obj) {
						textArea.setText(textArea.getText()+obj.getID()+"-->"+obj.getName()+"\n");
					}
					
				});
		jNavigator.add(operationProductCategoryNavigator, "operationCategory");
		
		JCustomerNavigator customerNavigator = new JCustomerNavigator(dlSales);
        customerNavigator.addCustomerChangedListener(new CustomerChangedListener(){

			
			public void valueSelectChanged(CustomerInfo obj) {
				textArea.setText(textArea.getText()+obj.getId()+"-->"+obj.getName()+"\n");
			}
        	
        });
        jNavigator.add(customerNavigator, "customer");
		
		
		JUserNavigator userNavigator=new JUserNavigator(dlSales);
        userNavigator.addUserChangedListener(new UserChangedListener(){

			
			public void valueSelectChanged(UserInfo obj) {
				textArea.setText(textArea.getText()+obj.getId()+"-->"+obj.getName()+"\n");	
			}
        	
        });
        jNavigator.add(userNavigator, "user");
        
        
        JLocationNavigator  locationNavigator=new JLocationNavigator(dlSales);
        locationNavigator.addLocationChangedListener(new LocationChangedListener(){
			
			public void valueSelectChanged(Location obj) {
				textArea.setText(textArea.getText()+obj.getID()+"-->"+obj.getName()+"\n");	
			}	
        });
        jNavigator.add(locationNavigator, "location");
        
        JButton button_2 = new JButton(AppLocal.getIntString("label.prodcategory"));
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl1.show(jNavigator, "category");
			}
		});
		navigatorToolBar.add(button_2);

		JButton btnNewButton = new JButton(
				AppLocal.getIntString("label.operationProductCategory"));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl1.show(jNavigator, "operationCategory");
			}
		});
		navigatorToolBar.add(btnNewButton);
        
        
        JButton button_1 = new JButton(AppLocal.getIntString("Menu.Users"));
        button_1.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		cl1.show(jNavigator, "user");
        	}
        });
        navigatorToolBar.add(button_1);
        
        
        JButton button = new JButton(AppLocal.getIntString("label.warehouse"));
        button.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		cl1.show(jNavigator, "location");
        	}
        });
        navigatorToolBar.add(button);
		
		JButton button_11 = new JButton(AppLocal.getIntString("label.customer"));
        button_11.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		cl1.show(jNavigator, "customer");
        	}
        });
        navigatorToolBar.add(button_11);
        
        
        try {
			productCategoryNavigator.loadData();
			operationProductCategoryNavigator.loadData();
	        customerNavigator.loadData();
	        userNavigator.loadData();
	        locationNavigator.loadData();
		} catch (BasicException e1) {
			e1.printStackTrace();
		}
	}
	
	protected  void configTabPanel(JTabbedPane tabbedPane){
		JSeachProductPanel seachProductPanel=new JSeachProductPanel(dlSales);
        tabbedPane.addTab(AppLocal.getIntString("label.search"), seachProductPanel);
        seachProductPanel.addProductChangedListener(new ProductChangedListener(){
			@Override
			public void baseBeanChanged(IKeyed<String> key, Product obj) {
				textArea.setText(textArea.getText()+key.getKey()+"-->"+"\n");
			}
        });
	}
	
}
